import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/13 16:51
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        int[] nums1 = {1,2,3,2,1}, nums2 = {3,2,1,4,7};
        Solution solution = new Solution();
        solution.findLength(nums1, nums2);
    }
    public int findLength(int[] A, int[] B) {
        int n = A.length, m = B.length;
        int ret = 0;
        for (int i = 0; i < n; i++) {
            int len = Math.min(m, n-i);
            int maxLen = maxLength(A, B, i, 0, len);
            ret = Math.max(ret, maxLen);
        }
        for (int i = 0; i < m; i++) {
            int len = Math.min(n, m-i);
            int maxLen = maxLength(A, B, 0, i, len);
            ret = Math.max(ret, maxLen);
        }
        return ret;
    }

    private int maxLength(int[] A, int[] B, int addA, int addB, int len) {
        int ret = 0, k = 0;
        for (int i = 0; i < len; i++) {
            if (A[addA+i] == B[addB+i]) {
                k++;
            }else {
                k = 0;
            }
            ret = Math.max(ret, k);
        }
        return ret;
    }
}
